package com.falcon.modules.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.falcon.modules.domain.bo.MyPage;
import com.falcon.modules.domain.bo.RoleSearchParam;
import com.falcon.modules.domain.bo.UserRoleMenuBo;
import com.falcon.modules.domain.bo.UserSearchParam;
import com.falcon.modules.domain.dto.Menu;
import com.falcon.modules.domain.dto.Role;
import com.falcon.modules.domain.dto.User;
import com.falcon.modules.domain.result.RestDataResult;
import com.falcon.modules.service.IMenuService;
import com.falcon.modules.service.IRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springside.modules.utils.mapper.JsonMapper;

import java.util.*;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;


    @RequestMapping("userRoleMenus")
    public List<UserRoleMenuBo> userRoleMenus(String menuIds) {
        if (StringUtils.isBlank(menuIds)) {
            return null;
        }

        List<Menu> allMenuList = menuService.selectList(null);

        System.out.println("###"+JsonMapper.INSTANCE.toJson(allMenuList));

        Set<Long> roleMenuIds = new HashSet<Long>();
        for (String menuIdStr : menuIds.split(",")) {
            try {
                if(StringUtils.isNotBlank(menuIdStr)) {
                    roleMenuIds.add(Long.parseLong(menuIdStr.trim()));
                }
            } catch (Exception e) {
            }
        }

        System.out.println("@!!!!"+JsonMapper.INSTANCE.toJson(roleMenuIds));

        List<UserRoleMenuBo> result = new ArrayList<UserRoleMenuBo>(allMenuList.size());
        for (Menu menu : allMenuList) {
            UserRoleMenuBo userRoleMenuBo = new UserRoleMenuBo(menu);
            if (menu != null && roleMenuIds.contains(menu.getId())) {
                userRoleMenuBo.setChecked(true);
            }
            result.add(userRoleMenuBo);
        }
        Collections.sort(result);
        System.out.println("==========" + JsonMapper.INSTANCE.toJson(result));
        return result;
    }

    @RequestMapping("enabledList")
    public JSONArray list() {
        Role role = new Role();
        role.setState(1);
        List<Role> list = roleService.selectList(new EntityWrapper<Role>(role));

        if (list != null && list.size() > 0) {
            JSONArray result = new JSONArray();
            for (Role r : list) {
                JSONObject item = new JSONObject();
                item.put("text", r.getName());
                item.put("value", r.getId());
                result.add(item);
            }
            return result;
        }
        return null;
    }

    @RequestMapping("list")
    public RestDataResult<Role> list(MyPage<Role> page,Role role) {
        Page<Role> rolePage = roleService.selectPage(page,new EntityWrapper(role));
        return new RestDataResult<Role>(rolePage.getRecords(), rolePage.getTotal());
    }


    @RequestMapping("selectBatchIds")
    public List selectBatchIds(Integer[] ids) {
        System.out.println(JsonMapper.INSTANCE.toJson(ids));
        return roleService.selectBatchIds(Arrays.asList(ids));
    }

    @RequestMapping("get")
    public Role get(Role role) {
        return roleService.selectOne(new EntityWrapper<Role>(role));
    }

    @RequestMapping("save")
    public boolean save(Role role){
        if(role == null){
            return false;
        }
        if(role.getId()!=null&&role.getId()>0){
            return roleService.updateById(role);
        }
        return roleService.insert(role);
    }

    @RequestMapping("del")
    public boolean del(Long id){
        if(id==null){
            return false;
        }
        return roleService.deleteById(id);
    }
}
